package controller;

import dao.StudentDao;
import domain.Student;
import service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    public void start() {
        //死循环
        Scanner sc = new Scanner(System.in);
        //死循环
        loop:
        while (true) {
            //键盘录入
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            //switch
            switch (choice) {
                case "1":
                    // System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                  deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    update();
                    break;
                case "4":
                    //System.out.println("查看学生");
                   find();
                    break;
                case "5":
                    System.out.println("退出学生系统,再见");
                    // break loop;
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }

    }

    public void update() {
        String sid = getString();
        Student student = getStudent(sid);
        studentService.update(sid,student);
        System.out.println("修改成功");
    }

    public void deleteStudent() {
        String sid = getString();
        studentService.delete(sid);
        System.out.println("删除成功");
    }

    public String getString() {
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            boolean flag = studentService.isExists(sid);
            if(flag){
                break;
            }else {
                System.out.println("不存在,请重新输入");
            }
        }
        return sid;
    }

    public void find() {
        ArrayList<Student> list=studentService.find();
        System.out.println(list);

    }

    public void addStudent() {
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            boolean flag = studentService.isExists(sid);
            if(flag){
                System.out.println("已经存在,请重新输入");
            }else {
               break;
            }
        }
        Student student = getStudent(sid);
        studentService.addStudent(student);
        System.out.println("添加成功");
    }

    public Student getStudent(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        return new Student(sid, name, age, birthday);
    }
}


